<?php

namespace Home\Controller;

use Think\Controller;
use Home\Model\GameModel;

class PayController extends Controller {

    private $gameModel;

    public function __construct() {
        parent::__construct();
        $this->gameModel = new GameModel;
    }

    public function index() {
        $paytype = M('paytype');
        $typelist = $paytype->where('flag=1')->order('listorder')->select();
        $gamelist = $this->gameModel->gameAll();

        $this->assign('gamelist', $gamelist);
        $this->assign('typelist', json_encode($typelist));
        $this->assign('payCss', 'on');
        $this->display();
    }

    public function gamelist() {
        $letterid = intval($_POST['letterid']);
        $gamelist = $this->gameModel->gameAll(0, $letterid);
        $this->ajaxReturn($gamelist, 'json');
    }

    public function serverlist() {
        $game_id = intval($_POST['game_id']);
        $serverlist = $this->gameModel->serverList(0, $game_id);
        $this->ajaxReturn($serverlist, 'json');
    }

    /**
     * 检查用户是否存在和对应的区服有角色
     */
    public function check_user() {
        $username = I('post.username');
        $game_id = intval($_POST['game_id']);
        $area_num = intval($_POST['area_num']);
        $useModel = new \Home\Model\UserModel();
        $relation = new \Home\Model\GameUserRelationModel();
        if ($useModel->getByUsername($username)) {
            if ($relation->getByRelation($username, $game_id, $area_num)) {
                $this->ajaxReturn(array(
                    'ret' => '0',
                    'msg' => '角色存在！'
                        ), 'json');
            } else {
                $this->ajaxReturn(array(
                    'ret' => '2',
                    'msg' => '角色不存在！'
                        ), 'json');
            }
        } else {
            $this->ajaxReturn(array(
                'ret' => '1',
                'msg' => '用户不存在！'
                    ), 'json');
        }
    }

    /**
     * 游戏币兑换比例
     */
    public function getexchange() {
        $game_id = intval($_POST['game_id']);
        $game = M('game_list');

        $data = $game->where("id='%d'", array($game_id))->find();
        $exchange = $data['cny_rate'];
        $this->ajaxReturn($exchange, 'json');
    }

    public function confirm() {
        if ($_GET['payid']) {
            $payid = intval($_GET['payid']);
        } else {
            $this->error("请选择充值渠道！", U('pay/index'));
        }

        if ($_GET['game_id']) {
            $game_id = intval($_GET['game_id']);
        } else {
            $this->error("请选择要充值的游戏！", U('pay/index'));
        }

        if ($_GET['area_num']) {
            $area_num = intval($_GET['area_num']);
        } else {
            $this->error("请选择要充值的区服！", U('pay/index'));
        }

        if ($_GET['username']) {
            $username = I('get.username');
        } else {
            $this->error("请输入要充值的账号！", U('pay/index'));
        }

        if ($_GET['username_to']) {
            $username_to = I('get.username_to');
        } else {
            $this->error("请输入要充值的账号！", U('pay/index'));
        }

        if ($_GET['money']) {
            $money = intval($_GET['money']);
        } else {
            $this->error("请选择充值金额！", U('pay/index'));
        }

        $bankname = $_GET['bankname'] ? I('get.bankname') : '';
        //判断充值渠道存不存在
        $paytype = M("paytype");
        $paytag = $paytype->field('id,typename,fee,paytag')->where("id='%d'", array($payid))->find();
        //var_dump($paytag);exit;
        if (!$paytag) {
            $this->error("充值渠道不存在，请重新选择充值渠道！", U('pay/index'));
        }
        //判断要充值的游戏区服存不存在
        $gameModel = new \Home\Model\GameModel();
        $server = $gameModel->serverList($area_num, $game_id);

        if (!$server) {
            $this->error("游戏区服不存在，请重新选择游戏区服！", U('pay/index'));
        }
        //判断用户名和角色存不存在
        $userModel = new \Home\Model\UserModel();
        $user = $userModel->getByUsername($username_to);
        //var_dump($user);
        if (!$user) {
            $this->error("你输入的用户名不存在，请重新输入！", U('pay/index'));
        }
//        $gameRelation = new \Home\Model\GameUserRelationModel();
//        $role = $gameRelation->getByRelation($username_to, $game_id, $area_num);
//        if (!$role) {
//            $this->error("你输入的用户名没有角色，请重新输入！", U('pay/index'));
//        }
        //组合充值数据
        $data['order_num'] = 'fu' . date('YmdHis') . rand(1000, 9999);
        if ($username == $username_to) {
            //玩家自己给自己充值
            $data['pay_uid'] = $user['uid'];
            $data['pay_username'] = $user['username'];
        } else {
            //其它人给玩家充值
            //username是充值人账号
            //username_to是被充值人账号
            $data['username'] = $username;
            $data['pay_uid'] = $user['uid'];
            $data['pay_username'] = $user['username'];
        }
        $data['paytypename'] = $paytag['typename'];
        $data['paytype'] = $payid;
        $data['pay_cny'] = $money;
        $data['fee_cny'] = $money * ((100 - $paytag['fee']) / 100); //根据充值渠道费换算实际充到游戏里的金额
        $data['fee_rate'] = $paytag['fee'];
        $data['pay_time'] = time();
        $data['flag_game'] = 0;
        $data['flag'] = 0;
        $data['pay_ip'] = get_client_ip(0, true);
        $data['gid'] = $game_id;
        $data['sid'] = $area_num;
        $data['discript'] = $server[0]['gamename'] . '|' . $server[0]['areaname'];

        //记录到充值表
        $payModel = new \Home\Model\PayModel();
        if ($payModel->create_pay_log($data)) {
            //根据paytag调用充值渠道
            //var_dump($paytag);
            //echo APP_PATH.'Home/Pay/'.$paytag['paytag'].'.class.php';
            require APP_PATH . 'Home/Pay/' . $paytag['paytag'] . '.class.php';
            $pay = new $paytag['paytag'];
            switch ($paytag['id']) {
                case 3:
                    $bankname = 'SZX-NET';
                    break;
                case 4:
                    $bankname = 'UNICOM-NET';
                    break;
                case 5:
                    $bankname = 'TELECOM-NET';
                    break;
                case 6:
                    $bankname = 'JUNNET-NET';
                    break;
                case 7:
                    $bankname = 'QQCARD-NET';
                    break;
                default:
                    break;
            }

            $get['username'] = $data['pay_username'];
            $get['uid'] = $data['pay_uid'];
            $get['paytype'] = $payid;
            $get['paytypename'] = $paytag['typename'];
            $get['pay_cny'] = $money;
            $get['gid'] = $game_id;
            $get['sid'] = $area_num;
            $get['platform'] = "248game";
            $get['order_num'] = $data['order_num'];
            $get['fee_cny'] = $money * ((100 - $paytag['fee']) / 100); //根据充值渠道费换算实际充到游戏里的金额
            $get['fee_rate'] = $paytag['fee'];
            //echo $get['gid'] . $get['sid'] . $get['uid'] . $get['money'] . $get['platform'] . "4a2024560102baa7bceb812f8938569a";exit;
            $get['sign'] = md5($get['gid'] . $get['sid'] . $get['uid'] . $get['pay_cny'] . $get['platform'] . "4a2024560102baa7bceb812f8938569a");
            $url = "http://www.678fu.com/api.php?s=pay/index&" . http_build_query($get);  
            
            header("Location:".$url);
            exit;




            $paydata = $pay->BeginPay($data, $bankname);
            //模板赋值
            $this->assign('data', $paydata);
            if ($paytag['paytag'] == 'yeepay') {
                //显示模板为gbk编码
                $this->display('yeepay', 'gbk');
            } else {
                $this->display($paytag['paytag']);
            }
        } else {
            $this->error("充值失败，请稍后在试！", U('pay/index'));
        }
    }

    public function callback() {
        $get = I('get.');
        //var_dump($get);exit;
        if ($get['paytag'] == 'yeepay') {
            $order_num = $get['r6_Order'];
        } else {
            $order_num = $get['out_trade_no'];
        }
        $paytag = $get['paytag'];
        $paymodel = new \Home\Model\PayModel();
        $payorder = $paymodel->getByOrderInfo($order_num);
        if (!$payorder) {
            $this->error('充值错误！', U('pay/index'));
        }
        require APP_PATH . 'Home/Pay/' . $paytag . '.class.php';
        $pay = new $paytag;
        $info = $pay->CallbackPay($get, $payorder, $paymodel);
        if ($info['ret'] != 0) {
            $this->error($info['msg'], U('pay/index'));
        }
        $gameModel = M('game_list');
        $gameTag = $gameModel->field('enname')->where("id='" . $payorder['gid'] . "'")->find();
        $class = ucfirst($gameTag['enname']);
        require_cache(APP_PATH . 'Home/Gameapi/' . $class . '.class.php');
        //var_dump($payorder);
        //需要根据订单获得游戏信息
        $serverModel = M('game_server_list');
        $server = $serverModel->where("area_num='%d' and game_id='%d'", array($payorder['sid'], $payorder['gid']))->find();
        // var_dump($server);exit;
        $result = $class::pay_to_game($server, $payorder);
        if ($result == 0) {
            if ($paymodel->setByOrderInfo($payorder['order_num'], 0, 0, 1, 1)) {
                $this->error("充值成功！", U('index/index'));
            } else {
                $this->error("充值失败！", U('pay/index'));
            }
        } else {
            $this->error("充值失败！", U('pay/index'));
        }
    }

    public function alipay_url() {
        $post = I('post.');
        $paymodel = new \Home\Model\PayModel();
        $payorder = $paymodel->getByOrderInfo($post['out_trade_no']);
        if (!$payorder) {
            $this->error('充值错误！', U('pay/index'));
        }
        require APP_PATH . 'Home/Pay/alipay.class.php';
        $pay = new alipay;
        $data = $pay->Notify_Url($post, $payorder);
    }

}
